/*
 * FileDAO.java
 *
 * Created on 18 marzec 2007, 18:46
 *
 */

package arocms.business;

import arocms.util.HibernateUtil;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Session;

/**
 *
 * @author arek
 */
public class FileDAO {
    
    private Logger log = Logger.getLogger(this.getClass());
    
    /** Creates a new instance of FileDAO */
    public FileDAO() {
    }
    
    public FileDetails findFileDetails(Long pk) {
        log.debug("--> findFileDetails");
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        FileDetails fd = new FileDetails();
        File f = (File) ses.load(File.class, pk);
        fd.setDescription(f.getDescription());
        fd.setFilename(f.getFilename());
        fd.setFilesize(f.getFilesize());
        fd.setFiletype(f.getFiletype());
        fd.setId(f.getId());
        fd.setTitle(f.getTitle());
        ses.getTransaction().commit();
        log.debug("<-- findFileDetails");
        return fd;
    }
    
    public InputStream getFileData(Long pk) throws SQLException{
        log.debug("--> getFileData");
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        File f = (File) ses.load(File.class, pk);
//        byte[] temp = f.getFilebin().getBytes(1L,(int)f.getFilebin().length());
//        InputStream blobIn = new ByteArrayInputStream(temp);
        InputStream blobIn = f.getFilebin().getBinaryStream();
        
        ses.getTransaction().commit();
        log.debug("<-- getFileData");
        return blobIn;
    }
    
    public void createOrUpdate(File aFile){
        log.debug("--> createOrUpdate");
        if (aFile.getId()==null || aFile.getId()==0) {
            create(aFile);
        } else update(aFile);
        log.debug("<-- createOrUpdate");
    }

    private void create(File aFile) {
        log.debug("--> create");
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        ses.persist(aFile);
        ses.getTransaction().commit();
        log.debug("<-- create");
    }

    private void update(File aFile) {
        log.debug("--> update");
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        ses.update(aFile);
        ses.getTransaction().commit();
        log.debug("<-- update");
    }
    
    public void delete(Long pk) {
        log.debug("--> delete");
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        ses.delete(ses.load(File.class,pk));
        ses.getTransaction().commit();
        log.debug("<-- delete");
    }

    public File findFile(Long pk) {
        log.debug("--> findFile");
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        File f = (File) ses.load(File.class, pk);
        ses.getTransaction().commit();
        log.debug("<-- findFile");
        return f;
    }

    public List findAll() {
        log.debug("--> findAll");
        Session ses = HibernateUtil.getSessionFactory().getCurrentSession();
        ses.beginTransaction();
        List lst = (List) ses.createQuery("SELECT id, filename, filesize FROM File AS f ORDER BY title ASC").list();
        ses.getTransaction().commit();
        log.debug("<-- findAll");
        return lst;
    }
}
